﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Text;
using Thor.Domain;
using Thor.Domain.Configuration;

namespace Thor.Repository.EntFx
{
    public class AdministratorAccountRepository : RepositoryBase, IAdministratorAccountRepository
    {
        #region Constructors

        public AdministratorAccountRepository(Dictionary<string, object> repositoryParameters)
            : base(repositoryParameters) { }

        #endregion

        #region IAdministratorAccountRepository Members

        public void Insert(AdministratorAccount administratorAccount)
        {
            if (administratorAccount != null && Model != null)
            {
                var entity = CreateEntityFromPoco(administratorAccount);
                Model.AddToAdministratorAccountEntities(entity);
                Model.SaveChanges();
            }
        }

        public void Update(AdministratorAccount administratorAccount)
        {
            if (administratorAccount != null && Model != null)
            {
                Delete(administratorAccount);
                Insert(administratorAccount);
            }
        }

        public void Delete(AdministratorAccount administratorAccount)
        {
            if (administratorAccount != null && Model != null)
            {
                var entity = Model.AdministratorAccountEntities.Where(a => a.Id == administratorAccount.Id);

                if (entity != null)
                {
                    Model.DeleteObject(entity);
                    Model.SaveChanges();
                }
            }
        }

        public List<AdministratorAccount> Fetch()
        {
            if (Model != null)
            {
                List<AdministratorAccount> result = new List<AdministratorAccount>();

                var entities = Model.AdministratorAccountEntities.ToList();

                if (entities != null)
                {
                    foreach (var entity in entities)
                        result.Add(CreatePocoFromEntity(entity));
                }

                return result;
            }

            return null;
        }

        public AdministratorAccount Fetch(Guid id)
        {
            if (Model != null)
            {
                var entity = Model.AdministratorAccountEntities.Where(a => a.Id == id).FirstOrDefault();

                if (entity != null)
                    return CreatePocoFromEntity(entity);
                else
                    return null;
            }

            return null;
        }

        #endregion

        #region Methods

        private AdministratorAccountEntity CreateEntityFromPoco(AdministratorAccount poco)
        {
            AdministratorAccountEntity entity = new AdministratorAccountEntity();

            entity.ApplicationName = ApplicationName;
            entity.EmailAddress = poco.EmailAddress;
            entity.FirstName = poco.FirstName;
            entity.Id = poco.Id;
            entity.LastName = poco.LastName;
            entity.UserName = poco.UserName;

            return entity;
        }

        private AdministratorAccount CreatePocoFromEntity(AdministratorAccountEntity entity)
        {
            AdministratorAccount poco = new AdministratorAccount();

            poco.EmailAddress = entity.EmailAddress;
            poco.FirstName = entity.FirstName;
            poco.Id = entity.Id;
            poco.LastName = entity.LastName;
            poco.UserName = entity.UserName;

            return poco;
        }

        #endregion
    }
}
